package com.alliky.main.ui.activity

import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.alibaba.android.arouter.launcher.ARouter
import com.alliky.common.router.Router
import com.alliky.core.base.BaseActivity
import com.alliky.core.utils.inTransaction
import com.alliky.home.mvp.contract.MainContract
import com.alliky.home.mvp.presenter.MainPresenter
import com.alliky.log.Logger
import com.alliky.main.R

import kotlinx.android.synthetic.main.activity_main.*
import me.majiajie.pagerbottomtabstrip.NavigationController
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener
import java.util.*

class MainActivity : BaseActivity<MainContract.View, MainContract.Presenter>(),
    MainContract.View {

    var mFragments = ArrayList<Fragment>()

    override fun createPresenter(): MainContract.Presenter = MainPresenter()

    override fun setLayout(): Any? = R.layout.activity_main

    override fun initView() {
        super.initView()
        Logger.d(">>>>","初始化成功了")
        initFragment()
        initBottomTab()
    }

    override fun initData() {
    }

    private fun initFragment() {
        //ARouter拿到多Fragment(这里需要通过ARouter获取，不能直接new,因为在组件独立运行时，宿主app是没有依赖其他组件，所以new不到其他组件的Fragment)
        val homeFragment =
            ARouter.getInstance().build(Router.Home.Fragment.HomeFragment).navigation() as Fragment
        val classifyFragment =
            ARouter.getInstance().build(Router.Classify.Fragment.ClassifyFragment)
                .navigation() as Fragment
        val cartFragment =
            ARouter.getInstance().build(Router.Cart.Fragment.CartFragment).navigation() as Fragment
        val userFragment =
            ARouter.getInstance().build(Router.User.Fragment.UserFragment).navigation() as Fragment

        mFragments.add(homeFragment)
        mFragments.add(classifyFragment)
        mFragments.add(cartFragment)
        mFragments.add(userFragment)
        //默认选中第一个
        supportFragmentManager.inTransaction {
            add(R.id.frame_layout, mFragments[0])
        }
    }

    private fun initBottomTab() {
        val navigationController: NavigationController = mPageNavigationView.material()
            .addItem(
                R.mipmap.icon_tab_home,
                "首页",
                ContextCompat.getColor(this, R.color.deepskyblue)
            )
            .addItem(
                R.mipmap.icon_tab_classify,
                "分类",
                ContextCompat.getColor(this, R.color.deepskyblue)
            )
            .addItem(
                R.mipmap.icon_tab_cart,
                "购物车",
                ContextCompat.getColor(this, R.color.deepskyblue)
            )
            .addItem(
                R.mipmap.icon_tab_my,
                "我的",
                ContextCompat.getColor(this, R.color.deepskyblue)
            )
            .build()
        //底部按钮的点击事件监听
        //底部按钮的点击事件监听
        navigationController.addTabItemSelectedListener(object : OnTabItemSelectedListener {
            override fun onSelected(index: Int, old: Int) {
                val currentFragment: Fragment = mFragments.get(index)
                val transaction = supportFragmentManager.beginTransaction()
                if (currentFragment != null) {
                    for (fragment in mFragments) {
                        transaction.hide(fragment!!)
                    }
                    addFragment(currentFragment, transaction)
                }
            }

            override fun onRepeat(index: Int) {}
        })
    }

    private fun addFragment(fragment: Fragment, transaction: FragmentTransaction) {
        if (!fragment.isAdded) {
            transaction.add(R.id.frame_layout, fragment)
        }
        transaction.show(fragment)
        transaction.commit()
    }
}